﻿//
// *************************************************************************************
// WP-Framework
//
// Developed by 
// Lukas Kretschmar (lkretschmar.dev@bluewin.ch)
//
// Switzerland, Copyright (c) 2013 lkretschmar
// This content is released under the MIT License (http://opensource.org/licenses/MIT).
//     
// *************************************************************************************
//
//


using Microsoft.VisualStudio.TestTools.UnitTesting;
using KretschIT.WP_Fx.EasyMoq.Test.Helpers.Objects;
using KretschIT.WP_Fx.Testing;

namespace KretschIT.WP_Fx.EasyMoq.Test.IntegrationTests
{
    [TestClass]
    public class PropertyTest
    {
        private IMock<ITestObject> mock;

        [TestInitialize]
        public void Init()
        {
            this.mock = Easy.Moq.Mock<ITestObject>();
        }

        [TestMethod]
        public void Test_Setup_Verify()
        {
            this.mock.SetupGet(m => m.Property);
            this.mock.VerifyGet(m => m.Property, Condition.Never());
        }

        [TestMethod]
        public void Test_Setup_Call_Verify()
        {
            this.mock.SetupGet(m => m.Property).Returns("test");

            var value = this.mock.Object.Property;

            this.mock.VerifyGet(m => m.Property, Condition.Once());
            Assert.AreEqual("test", value);
        }

        [TestMethod]
        public void Test_Setup_CallX_Verify()
        {
            this.mock.SetupGet(m => m.Property).Returns("test");

            var value = this.mock.Object.Property;
            value = this.mock.Object.Property;
            value = this.mock.Object.Property;

            this.mock.VerifyGet(m => m.Property, Condition.Exactly(3));
            Assert.AreEqual("test", value);
        }

        [TestMethod]
        public void Test_Setup_Throws_Verify()
        {
            this.mock.SetupGet(m => m.Property).Throws(new TestException());

            ExceptionAssert.Throws<TestException>(() =>
                {
                    var v = this.mock.Object.Property;
                });

            this.mock.VerifyGet(m => m.Property, Condition.Once());
        }

        [TestMethod]
        public void Test_Set_Get_Verify()
        {
            this.mock.Object.Property = "test";
            var value = this.mock.Object.Property;

            this.mock.VerifySet(m => m.Property, Condition.Once());
            this.mock.VerifyGet(m => m.Property, Condition.Once());
        }

        [TestMethod]
        public void Test_Setup_Throws_NotOverride_Verify()
        {
            this.mock.SetupGet(m => m.Property).Throws(new TestException(), false);

            this.mock.Object.Property = "test";
            ExceptionAssert.Throws<TestException>(() =>
            {
                var v = this.mock.Object.Property;
            });

            this.mock.VerifyGet(m => m.Property, Condition.Once());
            this.mock.VerifySet(m => m.Property, Condition.Once());
        }

        [TestMethod]
        public void Test_Setup_SetX_Get_Verify()
        {
            this.mock.SetupGet(m => m.Property).Returns("1");

            this.mock.Object.Property = "22";
            this.mock.Object.Property = "33";
            this.mock.Object.Property = "42";
            var value = this.mock.Object.Property;

            this.mock.VerifySet(m => m.Property, Condition.Exactly(3));
            this.mock.VerifyGet(m => m.Property, Condition.Once());
            Assert.AreEqual("42", value);
        }
    }
}
